package soar.test.db.mongodb;

import java.util.ArrayList;
import java.util.Date;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.QueryOperators;

public class App
{
    public static void main(String args[])
    {
        new App().run();
    }

    private void run()
    {
        MongoClient conn;
        DB db;

        try
        {
            conn = new MongoClient(Conf.mongodb.host, Conf.mongodb.port);
            db = conn.getDB(Conf.mongodb.database);
        }
        catch (Exception e)
        {
            System.err.println(String.format("connect fail: %s %s", e,
                    e.getMessage()));
            return;
        }

        try
        {
            String __split__ = "----------------------------------------";

            init(db);
            System.out.println(__split__);

            insert(db);
            System.out.println(__split__);

            select(db);
            System.out.println(__split__);

            delete(db);
            System.out.println(__split__);
        }
        catch (Exception e)
        {
            System.err.println(String.format("fail: %s", e));
        }

        try
        {
            conn.close();
        }
        catch (Exception e)
        {
        }
    }

    private void init(DB db)
    {
        final String __METHOD__="init";
        
        DBCollection test_users = db.getCollection("test_users");
        test_users.createIndex(new BasicDBObject("id", 1),
                new BasicDBObject("unique", true));
        test_users.createIndex(new BasicDBObject("login", 1),
                new BasicDBObject("unique", true));
        
        DBCollection test_seq=db.getCollection("test_seq");
        try
        {
            test_seq.insert(new BasicDBObject("id", "test_users").append(
                    "nextval", 111));
            test_seq.createIndex(new BasicDBObject("id", 1),
                    new BasicDBObject("unique", true));
        }
        catch(MongoException e)
        {
            System.err.println(String.format("%s: %s", __METHOD__, e));
        }
    }

    private void insert(DB db)
    {
        @SuppressWarnings("unused")
        final String __METHOD__ = "insert";

        DBCollection test_seq=db.getCollection("test_seq");
        DBCollection test_users = db.getCollection("test_users");

        String login = "JAVA000001@qq.com";
        DBObject user = new BasicDBObject();
        user.put("id", 101);
        user.put("update_ts", new Date());
        user.put("login", login);
        user.put("name", "JAVA东风");
        user.put("job", 1);
        user.put("address", "JAVA魔都");
        test_users.update(new BasicDBObject("login", login), user, true,
                false);
        
        ArrayList<DBObject> users=new ArrayList<>();
        for(int i=0; i<3; ++i)
        {
            DBObject seq=test_seq.findAndModify(new BasicDBObject("id", "test_users"),
                    new BasicDBObject("$inc", new BasicDBObject("nextval",
                            1)));
            int nextval=(Integer)seq.get("nextval");
            user = new BasicDBObject();
            user.put("id", nextval);
            user.put("update_ts", new Date());
            user.put("login", String.format("JAVA%06d@qq.com", nextval));
            user.put("name", "JAVA东风"+nextval);
            user.put("job", 2);
            user.put("address", "JAVA魔都"+nextval);
            users.add(user);
        }
        test_users.insert(users);
    }

    private void select(DB db)
    {
        @SuppressWarnings("unused")
        final String __METHOD__ = "select";

        DBCollection test_users = db.getCollection("test_users");

        DBCursor cur = test_users.find(
                new BasicDBObject("job", new BasicDBObject(
                        QueryOperators.GTE, 1))).sort(
                new BasicDBObject("id", -1));
        while (cur.hasNext())
        {
            DBObject rs = cur.next();
            long id = (Integer) rs.get("id");
            Date update_ts = (Date) rs.get("update_ts");
            String login = (String) rs.get("login");
            String name = (String) rs.get("name");
            int job = (Integer) rs.get("job");
            String address = (String) rs.get("address");
            System.out.println(String.format(
                    "%d | %s | %s | %s | %d | %s", id, update_ts, login,
                    name, job, address));
        }
        cur.close();
    }

    private void delete(DB db)
    {
        @SuppressWarnings("unused")
        final String __METHOD__ = "delete";

        DBCollection test_users = db.getCollection("test_users");

        test_users.remove(new BasicDBObject("id", 101));
    }
}
